﻿using System;
using MatchServer.Core;
using MatchServer.Packet.Handle;

namespace MatchServer.Packet
{
    class Handler
    {
        public static void PacketHandler (Client client, PacketReader packet)
        {
            try
            {
                switch (packet.getOpcode())
                {
                    case Operation.MatchLogin:
                        if (client.mClientFlags == PacketFlags.None)
                            Match.ProcessLogin(client, packet);
                        break;

                    case Operation.MatchRequestAccountCharList:
                        if (client.mClientFlags == PacketFlags.Login)
                            Match.ProcessCharacters(client, packet);
                        break;

                    case Operation.MatchRequestAccountCharInfo:
                        if (client.mClientFlags == PacketFlags.Login)
                            Match.ProcessCharInfo(client, packet);
                        break;

                    case Operation.MatchRequestSelectChar:
                        if (client.mClientFlags == PacketFlags.Login)
                            Match.ProcessSelectChar(client, packet);
                        break;

                    case Operation.MatchRequestCreateChar:
                        if (client.mClientFlags == PacketFlags.Login)
                            Match.ProcessCreateChar(client, packet);
                        break;

                    case Operation.MatchRequestDeleteChar:
                        if (client.mClientFlags == PacketFlags.Login)
                            Match.ProcessDeleteChar(client, packet);
                        break;

                    case Operation.MatchRequestRecommendedChannel:
                        if (client.mClientFlags >= PacketFlags.Character)
                            Match.ProcessRequestRecommendChannel(client, packet);
                        break;

                    case Operation.MatchRequestCharacterItemList:
                        if (client.mClientFlags >= PacketFlags.Character)
                            Match.ProcessCharItemList(client, packet);
                        break;

                    case Operation.MatchRequestShopItemList:
                        if (client.mClientFlags >= PacketFlags.Character)
                            Match.ProcessShopItemList(client, packet);
                        break;

                    case Operation.MatchRequestBuyItem:
                        if (client.mClientFlags >= PacketFlags.Character)
                            Match.ProcessBuyItem(client, packet);
                        break;

                    case Operation.MatchRequestSellItem:
                        if (client.mClientFlags >= PacketFlags.Character)
                            Match.ProcessSellItem(client, packet);
                        break;

                    case Operation.MatchRequestEquipItem:
                        if (client.mClientFlags >= PacketFlags.Character)
                            Match.ProcessEquipItem(client, packet);
                        break;

                    case Operation.MatchRequestTakeOffItem:
                        if (client.mClientFlags >= PacketFlags.Character)
                            Match.ProcessTakeOffItem(client, packet);
                        break;

                    case Operation.ChannelJoin:
                        if (client.mClientFlags == PacketFlags.Character)
                            Channel.ResponseChannelJoin(client, packet);
                        break;

                    case Operation.ChannelRequestChat:
                        if (client.mClientFlags == PacketFlags.Character)
                            Channel.ResponseChannelChat(client, packet);
                        break;

                    case Operation.ChannelRequestJoinFromName:
                        if (client.mClientFlags == PacketFlags.Character)
                            Channel.ResponseChannelJoinFromName(client, packet);
                        break;

                    case Operation.ChannelRequestPlayerList:
                        if (client.mClientFlags == PacketFlags.Character)
                            Channel.ResponsePlayerList(client, packet);
                        break;

                    case Operation.ChannelListStart:
                        if (client.mClientFlags == PacketFlags.Character)
                            Channel.ResponseChannelList(client, packet);
                        break;

                    case Operation.StageCreate:
                        if (client.mClientFlags == PacketFlags.Character)
                            Stage.ProcessStageCreate(client, packet);
                        break;

                    case Operation.StageListRequest:
                        if (client.mClientFlags == PacketFlags.Character)
                            Stage.ProcessStageList(client, packet);
                        break;

                    case Operation.StageRequestJoin:
                        if (client.mClientFlags == PacketFlags.Character)
                            Stage.ProcessStageJoin(client, packet);
                        break;

                    case Operation.StageList:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Stage.ProcessStageLeave(client, packet);
                        break;

                    case Operation.StageChat:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Stage.ResponseStageChat(client, packet);
                        break;

                    case Operation.StageMap:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Stage.ProcessStageMap(client, packet);
                        break;

                    case Operation.StageRequestSettings:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Stage.ProcessStageSettings(client, packet);
                        break;

                    case Operation.StageUpdateSettings:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Stage.ProcessStageSetting(client, packet);
                        break;

                    case Operation.StageTeam:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Stage.ProcessStageTeam(client, packet);
                        break;

                    case Operation.StageState:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Stage.ProcessStageState(client, packet);
                        break;

                    case Operation.StageStart:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Stage.ProcessStageStart(client, packet);
                        break;
                    
                    case Operation.LoadingComplete:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Battle.ProcessLoading(client, packet);
                        break;

                    case Operation.StageRequestEnterBattle:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Battle.ProcessEnterBattle(client, packet);
                        break;

                    case Operation.BattleRequestInfo:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Battle.ProcessBattleInfo(client, packet);
                        break;

                    case Operation.GameRequestSpawn:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Battle.ProcessGameSpawn(client, packet);
                        break;

                    case Operation.GameRequestTimeSync:
                        if (client.mClientFlags == PacketFlags.Stage)
                            Battle.ProcessGameTimeSync(client, packet);
                        break;
                    default:
                        Log.Write("Received unhandled packet: {0}", packet.getOpcode());
                        break;
                }
            }
            catch (Exception e)
            {
                Log.Write("Parse error: {0}", packet.getOpcode());
            }
        }
    }
}
